package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.SecurityquestionForm;
import com.vcaresolution.logistic.to.SecurityquestionTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>SecurityquestionController</code> is used to track all login related
 * actions
 * 
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class SecurityquestionController extends BaseController {

	@ModelAttribute(value = ApplicationConstants.SECURITYQUESTIONFORM_MODELATTRIBUTE)
	public SecurityquestionForm createForm() {
		return new SecurityquestionForm();
	}

	/**
	 * This method is called when the GET request has been made to the ADDSector
	 * Request
	 */
	@RequestMapping(value = ApplicationConstants.ADD_SECURITYQUESTION_URL, method = RequestMethod.GET)
	public ModelAndView loadAddSecurityquestion() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView
				.setViewName(ApplicationConstants.ADD_SECURITYQUESTION_VIEW);
		modelAndView.addObject(
				ApplicationConstants.SECURITYQUESTIONFORM_MODELATTRIBUTE,
				new SecurityquestionForm());
		return modelAndView;
	}

	/**
	 * This method is called when the POST request has been made to the
	 * ADDSector Request
	 */
	@RequestMapping(value = ApplicationConstants.ADD_SECURITYQUESTION_URL, method = RequestMethod.POST)
	public ModelAndView processAddSector(
			@Valid SecurityquestionForm securityquestionForm,
			final BindingResult result, final HttpServletRequest request,
			final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();

		System.out.println("Inside Controller :: " + result.hasErrors());

		if (!result.hasErrors()) {
			getServiceRegistry().getSecurityquestionService().add(
					securityquestionForm);

			modelAndView.setView(new RedirectView(
					ApplicationConstants.LIST_SECURITYQUESTION_URL, true, true,
					false));
		}
		return modelAndView;
	}

	/**
	 * This method is called when the GET request has been made to the
	 * ListSector Request
	 */
	@RequestMapping(value = ApplicationConstants.LIST_SECURITYQUESTION_URL, method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();

		List<SecurityquestionTO> securityquestionList = getServiceRegistry()
				.getSecurityquestionService().findByAll();

		modelAndView.addObject(
				ApplicationConstants.SECURITYQUESTIONLIST_MODELATTRIBUTE,
				securityquestionList);
		modelAndView
				.setViewName(ApplicationConstants.LIST_SECURITYQUESTION_VIEW);
		return modelAndView;
	}

	/**
	 * This method is called when the GET request has been made to the ADD
	 * Securityquestion Request
	 */
	@RequestMapping(value = ApplicationConstants.EDIT_SECURITYQUESTION_URL, method = RequestMethod.GET)
	public ModelAndView loadEditSecurityquestion(@RequestParam(value = "sqId") String sqID) {
		ModelAndView modelAndView = new ModelAndView();

		Object securityquestionObject = getServiceRegistry().getSecurityquestionService().findById(Integer.parseInt(sqID));

		modelAndView.addObject(
				ApplicationConstants.SECURITYQUESTIONFORM_MODELATTRIBUTE,
				securityquestionObject);
		modelAndView
				.setViewName(ApplicationConstants.EDIT_SECURITYQUESTION_VIEW);

		return modelAndView;
	}

	/**
	 * This method is called when the POST request has been made to the
	 * ADD Security Question Request
	 */
	@RequestMapping(value = ApplicationConstants.EDIT_SECURITYQUESTION_URL, method = RequestMethod.POST)
	public ModelAndView processEditSecurityquestionForm(
			@Valid SecurityquestionForm securityquestionForm,
			final BindingResult result, final HttpServletRequest request,
			final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();

		System.out.println("Inside Controller :: " + result.hasErrors());

		if (!result.hasErrors()) {
			getServiceRegistry().getSecurityquestionService().update(
					securityquestionForm);

			modelAndView.setView(new RedirectView(
					ApplicationConstants.LIST_SECURITYQUESTION_URL, true, true,
					false));
		}
		return modelAndView;
	}
}